package cn.zhentao.service.impl;

import cn.zhentao.book.dto.Booklist;
import cn.zhentao.common.Result;
import cn.zhentao.dtos.ResponseResult;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhentao.book.pojo.TBook;
import cn.zhentao.service.TBookService;
import cn.zhentao.mapper.TBookMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author EWEWW
* @description 针对表【t_book】的数据库操作Service实现
* @createDate 2024-06-29 09:39:46
*/
@Service
public class TBookServiceImpl extends ServiceImpl<TBookMapper, TBook>
    implements TBookService{
    @Autowired
    private TBookMapper tBookMapper;

    //查询图书以及所有条件
    @Override
    public Page<TBook> listBook(Booklist booklist) {
        Page<TBook> page = new Page<>(booklist.getPagenum(), booklist.getPagesize());
        Page<TBook> list = tBookMapper.listBook(page,booklist);
        return list;
    }

    //查询单个图书
    @Override
    public Result selctone(Integer id) {
        TBook one = tBookMapper.selctOne(id);
        return Result.OK(one);
    }
    //图书添加
    @Override
    public Result book_add(TBook book) {
        //判断参数是否为空
        if(StringUtils.isBlank(book.getName())){
            return Result.ERROR("图书名称不能为空哦");
        }
//         this.saveOrUpdate(book);
        if(book.getId() == null){
            //判断是否有该图书
            TBook tBook = tBookMapper.selectOne(Wrappers.<TBook>lambdaQuery().eq(TBook::getName, book.getName()));
            if(tBook != null){
                return Result.ERROR("该书已经存在，换一本吧");
            }
            tBookMapper.insert(book);
            return Result.OK("添加成功");
        }else{
            tBookMapper.updateById(book);
            return Result.OK("编辑成功");
        }
    }
    //书上下架
    @Override
    public ResponseResult book_upOrdown(TBook book) {
        if(book.getId()==null || book.getStatus() == null){
            return ResponseResult.errorResult(500, "参数不能为空");
        }
        TBook books = this.getById(book.getId());
        if(book.getStatus()==0 && books.getStatus()==0){  //0是上架  传过来的是0 数据库查出来也是0  说明要下架操作
            books.setStatus(1);
            this.updateById(books);
            return ResponseResult.okResult("设置下架成功");
        }else if(book.getStatus()==1 && books.getStatus()==1){  //上架操作
            books.setStatus(0);
            this.updateById(books);
        }
        return ResponseResult.okResult("设置上架成功");
    }
    //租不租借图书
    @Override
    public ResponseResult book_lease(TBook book) {
        if(book.getId()==null || book.getStatus() == null){
            return ResponseResult.errorResult(500, "参数不能为空");
        }
        TBook books = this.getById(book.getId());
        if(book.getStatus()==0 && books.getStatus()==0){  //0是正常  传过来的是0 数据库查出来也是0  说明要不租借操作
            books.setStatus(2);
            this.updateById(books);
            return ResponseResult.okResult("设置不租借成功");
        }else if(book.getStatus()==2 && books.getStatus()==2){  //租借操作
            books.setStatus(0);
            this.updateById(books);
        }
        return ResponseResult.okResult("设置租借成功");
    }
}




